package com.example.familylibrary.service.impl;

import com.example.familylibrary.dto.BookDTO;
import com.example.familylibrary.entity.BookEntity;
import com.example.familylibrary.repository.BookRepository;
import com.example.familylibrary.service.BookService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookRepository bookRepository;

    @Override
    public BookDTO createBook(BookDTO bookDTO) {
        BookEntity bookEntity = new BookEntity();
        convertToEntity(bookDTO, bookEntity);
        
        BookEntity savedBook = bookRepository.save(bookEntity);
        
        BookDTO result = new BookDTO();
        convertToDTO(savedBook, result);
        return result;
    }

    @Override
    public BookDTO getBookById(Long id) {
        BookEntity bookEntity = bookRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("图书不存在"));
        
        BookDTO result = new BookDTO();
        convertToDTO(bookEntity, result);
        return result;
    }

    @Override
    public List<BookDTO> getAllBooks() {
        return bookRepository.findAll().stream().map(bookEntity -> {
            BookDTO bookDTO = new BookDTO();
            convertToDTO(bookEntity, bookDTO);
            return bookDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<BookDTO> getBooksByUserId(Long userId) {
        return bookRepository.findByUserId(userId).stream().map(bookEntity -> {
            BookDTO bookDTO = new BookDTO();
            convertToDTO(bookEntity, bookDTO);
            return bookDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<BookDTO> searchBooksByTitle(Long userId, String title) {
        return bookRepository.findByUserIdAndTitleContaining(userId, title).stream().map(bookEntity -> {
            BookDTO bookDTO = new BookDTO();
            convertToDTO(bookEntity, bookDTO);
            return bookDTO;
        }).collect(Collectors.toList());
    }

    @Override
    public BookDTO updateBook(Long id, BookDTO bookDTO) {
        BookEntity bookEntity = bookRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("图书不存在"));
        
        convertToEntity(bookDTO, bookEntity);
        
        BookEntity updatedBook = bookRepository.save(bookEntity);
        
        BookDTO result = new BookDTO();
        convertToDTO(updatedBook, result);
        return result;
    }

    @Override
    public void deleteBook(Long id) {
        if (!bookRepository.existsById(id)) {
            throw new RuntimeException("图书不存在");
        }
        bookRepository.deleteById(id);
    }

    private void convertToEntity(BookDTO bookDTO, BookEntity bookEntity) {
        bookEntity.setIsbn(bookDTO.getIsbn());
        bookEntity.setTitle(bookDTO.getTitle());
        bookEntity.setAuthor(bookDTO.getAuthor());
        bookEntity.setPublisher(bookDTO.getPublisher());
        bookEntity.setPublishDate(bookDTO.getPublishDate());
        bookEntity.setCoverUrl(bookDTO.getCoverUrl());
        bookEntity.setDescription(bookDTO.getDescription());
        bookEntity.setCategoryId(bookDTO.getCategoryId());
        bookEntity.setTags(bookDTO.getTags());
        bookEntity.setPages(bookDTO.getPages());
        bookEntity.setLanguage(bookDTO.getLanguage());
        bookEntity.setUserId(bookDTO.getUserId());
        bookEntity.setStatus(bookDTO.getStatus() != null ? bookDTO.getStatus() : 1);
    }

    private void convertToDTO(BookEntity bookEntity, BookDTO bookDTO) {
        bookDTO.setId(bookEntity.getId());
        bookDTO.setIsbn(bookEntity.getIsbn());
        bookDTO.setTitle(bookEntity.getTitle());
        bookDTO.setAuthor(bookEntity.getAuthor());
        bookDTO.setPublisher(bookEntity.getPublisher());
        bookDTO.setPublishDate(bookEntity.getPublishDate());
        bookDTO.setCoverUrl(bookEntity.getCoverUrl());
        bookDTO.setDescription(bookEntity.getDescription());
        bookDTO.setCategoryId(bookEntity.getCategoryId());
        bookDTO.setTags(bookEntity.getTags());
        bookDTO.setPages(bookEntity.getPages());
        bookDTO.setLanguage(bookEntity.getLanguage());
        bookDTO.setUserId(bookEntity.getUserId());
        bookDTO.setCreateTime(bookEntity.getCreateTime());
        bookDTO.setUpdateTime(bookEntity.getUpdateTime());
        bookDTO.setStatus(bookEntity.getStatus());
    }
}